package com.example.workassignback.Controller;

import com.example.workassignback.POJO.Doctor;
import com.example.workassignback.annotation.CurrentUser;
import com.example.workassignback.serviceImpl.service.DoctorService;
import com.example.workassignback.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Auther: Lai Wenhao
 * @Date: 2024/6/1 11:21
 **/
@CrossOrigin
@Controller
@ResponseBody
@RequestMapping("/doctor")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @GetMapping("/getInfo")
    public Result getInfo(@CurrentUser String id) {
        return doctorService.getInfo(id);
    }

    @GetMapping("/findPatient")
    public Result findPatient(@RequestBody Map<String, ?> map) {
        String findType = (String) map.get("findType");
        String findKey = (String) map.get("findKey");
//        System.out.println("findKey = " + findKey);
//        System.out.println("findType = " + findType);
        return Result.success(doctorService.getPatient(findType, findKey));

    }

    @GetMapping("/getBeginItems")
    public Result GetBeginItems(@CurrentUser String id) {
        return doctorService.GetBeginItemsByStatus(id);
    }

    @GetMapping("/modifyInfo")
    public Result modifyInfo(@CurrentUser String id, @RequestBody Map<String, ?> map) {
        String password=(String) map.get("password");
        String phone = (String) map.get("phone");
        String homeAddress = (String) map.get("homeAddress");
        String dateString = (String) map.get("birthday");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/M/d");
        Date birthday;
        try {
            birthday= formatter.parse(dateString);
            System.out.println("Date: " + birthday);
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.fail("参数异常");
        }

        Doctor doctor=new Doctor();
        doctor.setBirthday(birthday);
        doctor.setPhone(phone);
        doctor.setPassword(password);
        doctor.setHomeAddress(homeAddress);
        doctor.setUId(Integer.valueOf(id));

        return doctorService.modifyInfo(doctor);
    }
}
